document.addEventListener('DOMContentLoaded', function() {
    const passwordInput = document.getElementById('newPassword');
    const strengthIndicator = document.getElementById('strengthIndicator');

    passwordInput.addEventListener('input', function() {
        const strength = calculatePasswordStrength(this.value);
        updateStrengthIndicator(strength);
    });


    function calculatePasswordStrength(password) {
        let strength = 0;
        if (password.length >= 8) strength += 1;
        if (password.match(/[A-Z]/)) strength += 1;
        if (password.match(/[0-9]/)) strength += 1;
        if (password.match(/[^A-Za-z0-9]/)) strength += 1;
        return Math.min(strength, 4);
    }

    function updateStrengthIndicator(strength) {
        const colors = ['#e74c3c', '#f1c40f', '#3498db', '#2ecc71'];
        const widths = ['25%', '50%', '75%', '100%'];
        strengthIndicator.style.backgroundColor = colors[strength - 1];
        strengthIndicator.style.width = widths[strength - 1];
    }
});

document.querySelector('.btn-submit').addEventListener('click', async function(e) {
    e.preventDefault(); // 阻止默认表单提交

    const formData = {
        currentPassword: document.querySelector('input[type="password"]').value, // 当前密码输入框
        newPassword: document.getElementById('newPassword').value,
        confirmPassword: document.querySelectorAll('input[type="password"]')[2].value
    };
    console.log('Form data:', formData);

    // 前端验证
    if (formData.newPassword !== formData.confirmPassword) {
       showToast('两次密码输入不一致', 'error');
        return;
    }
    $.ajax({
        url: 'http://localhost:9220/AttendanceSystem/ChangePasswordController',
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify(formData),
        dataType: 'json',
        success: function(response) {
            showToast('修改成功', 'success')
        },
        error: function(xhr, status, error) {
            showToast('修改失败', 'error')
        }
    });


});